package com.cema.manage.project.manage.questionBank.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.framework.web.domain.BaseEntity;
import com.cema.manage.project.manage.questionBank.domain.QuestionBank;
import com.cema.manage.project.manage.questionBank.service.IQuestionBankService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cema.manage.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static java.sql.Types.NUMERIC;
import static org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType.FORMULA;


/**
 * 成语题库 信息操作处理
 *
 * @author reasahi
 * @date 2024-07-12
 */
@Controller
@RequestMapping("/manage/questionBank")
public class QuestionBankController extends BaseController {
    private String prefix = "manage/questionBank";

    @Autowired
    private IQuestionBankService questionBankService;

    @GetMapping()
    @RequiresPermissions("manage:questionBank:view")
    public String questionBank() {
        return prefix + "/questionBank";
    }

    /**
     * 查询成语题库列表
     */
    @RequiresPermissions("manage:questionBank:list")
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(BaseEntity baseEntity) {
        startPage();
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.like("qb_content",baseEntity.getSearchValue());
        List<QuestionBank> list = questionBankService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 新增成语题库
     */
    @RequiresPermissions("manage:questionBank:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改成语题库
     */
    @RequiresPermissions("manage:questionBank:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        QuestionBank questionBank = questionBankService.selectById(id);
        model.addAttribute("questionBank", questionBank);
        return prefix + "/edit";
    }

    /**
     * 保存成语题库
     */
    @RequiresPermissions("manage:questionBank:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(QuestionBank questionBank) {
        Integer id = questionBank.getId();
        if (StringUtils.isNotNull(id)) {
            if (questionBankService.updateById(questionBank)) {
                return Message.success();
            }
            return Message.error();
        } else {
            if (questionBankService.insert(questionBank)) {
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除成语题库
     */
    @RequiresPermissions("manage:questionBank:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        if (questionBankService.deleteById(id)) {
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除成语题库
     */
    @RequiresPermissions("manage:questionBank:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[] ids) {
        List list = new ArrayList();
        for (Integer n : ids) {
            list.add(n);
        }
        if (questionBankService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }


    @ResponseBody
    @PostMapping("/upload")
    public Message uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        if (ObjectUtil.isNull(file) || file.isEmpty()) {
            return Message.error("请选择文件");
        }
        InputStream fileInputStream = file.getInputStream();
        questionBankService.redXls(fileInputStream);
        return Message.success();
    }

}
